package com.fyb.basic.juc.cyclicBarrier;

import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @Author：MR.FANG
 * @name：Test
 * @Date：2025/7/2 下午2:50
 */
public class Test {

    private final static int gradeNum = 6;

    private static CyclicBarrier cyclicBarrier = new CyclicBarrier(3);

    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService = Executors.newScheduledThreadPool(gradeNum);

        System.out.println(" 通知，通知，通知，请准备好的年纪先出发。。。");

        for (int i = 0; i < gradeNum; i++) {
            TimeUnit.SECONDS.sleep(1);
            int gradeName = i + 1;
            executorService.submit( () -> {
                try {
                    wait(gradeName);
                }catch (Exception e){

                }
            });
        }
    }

    private static void wait(int gradeName) throws Exception{
        TimeUnit.SECONDS.sleep(1);

        System.out.println(gradeName + "年纪所有同学来到了出口。。。。。");

        cyclicBarrier.await();

        System.out.println(gradeName + "年纪所有同学到出发");
    }
}
